Main site

Sample codes > Carrier Custom Rates

Carrier Custom Rates

The following sample code defines custom rate for a carrier, Australia Post, for postal code zones of Australia. This showcases how JsRates handles postal code based rates.

Start by creating a module for the data and rate calculator of Australia Post.

  • Head to the Editor page of the app, click the new file button on the editor tab, rename the module to Austpost.js.
  • Paste the following codes to the newly created files and save.

Austpost code

const austPostZones = [ {'start':200,'end':299,'zone':'CB'},{'start':800,'end':802,'zone':'NT1'},{'start':803,'end':803,'zone':'NT2'},{'start':804,'end':821,'zone':'NT1'},{'start':822,'end':827,'zone':'NT2'},{'start':828,'end':851,'zone':'NT1'},{'start':852,'end':852,'zone':'NT2'},{'start':853,'end':853,'zone':'NT1'},{'start':854,'end':859,'zone':'NT2'},{'start':860,'end':861,'zone':'NT1'},{'start':862,'end':869,'zone':'NT2'},{'start':870,'end':871,'zone':'NT1'},{'start':872,'end':872,'zone':'NT2'},{'start':873,'end':879,'zone':'NT1'},{'start':880,'end':905,'zone':'NT2'},{'start':906,'end':999,'zone':'NT1'},{'start':1000,'end':1935,'zone':'N1'},{'start':1936,'end':1999,'zone':'N3'},{'start':2000,'end':2249,'zone':'N1'},{'start':2250,'end':2263,'zone':'GF'},{'start':2264,'end':2281,'zone':'N2'},{'start':2282,'end':2310,'zone':'NC'},{'start':2311,'end':2484,'zone':'N2'},{'start':2485,'end':2486,'zone':'N4'},{'start':2487,'end':2499,'zone':'N2'},{'start':2500,'end':2507,'zone':'WG'},{'start':2508,'end':2514,'zone':'N1'},{'start':2515,'end':2532,'zone':'WG'},{'start':2533,'end':2554,'zone':'N2'},{'start':2555,'end':2574,'zone':'N1'},{'start':2575,'end':2599,'zone':'N2'},{'start':2600,'end':2620,'zone':'CB'},{'start':2621,'end':2639,'zone':'N2'},{'start':2640,'end':2641,'zone':'N3'},{'start':2642,'end':2647,'zone':'N2'},{'start':2648,'end':2648,'zone':'V2'},{'start':2649,'end':2707,'zone':'N2'},{'start':2708,'end':2709,'zone':'N3'},{'start':2710,'end':2714,'zone':'N2'},{'start':2715,'end':2715,'zone':'V2'},{'start':2716,'end':2716,'zone':'N2'},{'start':2717,'end':2719,'zone':'V2'},{'start':2720,'end':2730,'zone':'N2'},{'start':2731,'end':2739,'zone':'V2'},{'start':2740,'end':2786,'zone':'N1'},{'start':2787,'end':2879,'zone':'N2'},{'start':2880,'end':2889,'zone':'S2'},{'start':2890,'end':2897,'zone':'N1'},{'start':2898,'end':2899,'zone':'NF'},{'start':2900,'end':2920,'zone':'CB'},{'start':3000,'end':3210,'zone':'V1'},{'start':3211,'end':3220,'zone':'GL'},{'start':3221,'end':3334,'zone':'V2'},{'start':3335,'end':3341,'zone':'V1'},{'start':3342,'end':3349,'zone':'V2'},{'start':3350,'end':3350,'zone':'BR'},{'start':3351,'end':3352,'zone':'V2'},{'start':3353,'end':3356,'zone':'BR'},{'start':3357,'end':3426,'zone':'V2'},{'start':3427,'end':3443,'zone':'V1'},{'start':3444,'end':3688,'zone':'V2'},{'start':3689,'end':3690,'zone':'V3'},{'start':3691,'end':3749,'zone':'V2'},{'start':3750,'end':3811,'zone':'V1'},{'start':3812,'end':3909,'zone':'V2'},{'start':3910,'end':3920,'zone':'V1'},{'start':3921,'end':3925,'zone':'V2'},{'start':3926,'end':3944,'zone':'V1'},{'start':3945,'end':3974,'zone':'V2'},{'start':3975,'end':3978,'zone':'V1'},{'start':3979,'end':3979,'zone':'V2'},{'start':3980,'end':3983,'zone':'V1'},{'start':3984,'end':3999,'zone':'V2'},{'start':4000,'end':4209,'zone':'Q1'},{'start':4210,'end':4224,'zone':'GC'},{'start':4225,'end':4225,'zone':'Q5'},{'start':4226,'end':4269,'zone':'GC'},{'start':4270,'end':4299,'zone':'Q1'},{'start':4300,'end':4308,'zone':'IP'},{'start':4309,'end':4453,'zone':'Q2'},{'start':4454,'end':4499,'zone':'Q3'},{'start':4500,'end':4549,'zone':'Q1'},{'start':4550,'end':4579,'zone':'SC'},{'start':4580,'end':4693,'zone':'Q2'},{'start':4694,'end':4802,'zone':'Q3'},{'start':4803,'end':4803,'zone':'Q4'},{'start':4804,'end':4805,'zone':'Q3'},{'start':4806,'end':4999,'zone':'Q4'},{'start':5000,'end':5200,'zone':'S1'},{'start':5201,'end':5749,'zone':'S2'},{'start':5800,'end':5999,'zone':'S1'},{'start':6000,'end':6214,'zone':'W1'},{'start':6215,'end':6700,'zone':'W2'},{'start':6701,'end':6797,'zone':'W3'},{'start':6798,'end':6799,'zone':'W4'},{'start':6800,'end':6999,'zone':'W1'},{'start':7000,'end':7150,'zone':'T1'},{'start':7151,'end':7154,'zone':'AAT'},{'start':7155,'end':7999,'zone':'T1'},{'start':8000,'end':8999,'zone':'V1'},{'start':9000,'end':9725,'zone':'Q1'},{'start':9726,'end':9799,'zone':'GC'},{'start':9880,'end':9919,'zone':'GC'},{'start':9920,'end':9960,'zone':'Q3'},{'start':9961,'end':9998,'zone':'Q4'},{'start':9999,'end':9999,'zone':'V1'}];

const ausPostRates = {
    "zones":["N0","N1","GF","WG","NC","CB","N3","N4","N2","V0","V1","GL","BR","V3","V2","Q0","Q1","IP","GC","Q5","SC","Q2","Q3","Q4","S0","S1","S2","W0","W1","W2","W3","T0","T1","NT1","NT2","NF","W4","AAT"],
   	"fixed":[16.66,16.66,15.55,15.55,16.66,16.66,14.85,18.86,18.86,18.71,24.74,16.29,16.97,15.84,15.84,16.66,16.66,17.62,15.55,20.12,17.62,18.86,18.86,21.55,17.94,17.94,21.55,17.62,17.62,16.76,20.12,15.54,14.68,16.76,16.76,17.27,20.12,19.74],
    "variable":[0.90,0.90,1.15,1.15,1.23,1.23,0.90,1.28,1.28,0,0,0.58,1.02,1.01,1.01,1.15,1.15,2.15,2.15,1.32,2.15,2.45,2.89,3.72,0.97,0.97,1.83,2.44,2.44,3.92,5.23,1.01,0.96,4.32,4.32,3.94,4.76,1.37],
    "add_subsequent":[14.29,14.29,13.34,13.34,14.29,14.29,12.78,16.79,16.79,14.57,20.61,13.93,14.61,13.64,13.64,14.29,14.29,15.41,13.34,17.91,15.41,16.79,16.79,19.19,15.39,15.39,19.19,15.41,15.41,14.93,17.91,13.6,12.84,14.93,14.93,15.21,17.91,17.38]
}

const ausPostInterval = {
    "weightLower":[0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98],
    "weightUpper":[1.9,3.9,5.9,7.9,9.9,11.9,13.9,15.9,17.9,19.9,21.9,23.9,25.9,27.9,29.9,31.9,33.9,35.9,37.9,39.9,41.9,43.9,45.9,47.9,49.9,51.9,53.9,55.9,57.9,59.9,61.9,63.9,65.9,67.9,69.9,71.9,73.9,75.9,77.9,79.9,81.9,83.9,85.9,87.9,89.9,91.9,93.9,95.9,97.9,99.9]
}

export const getAusPostRates = (postcode,weight) =>{
    const index = ausPostInterval.weightLower.findLastIndex((lower) => lower <= weight );
    const G4 = ausPostInterval.weightLower[index];
    const G5 = ausPostInterval.weightUpper[index];
    
    const zone = austPostZones.find(z => postcode >= z.start && postcode <= z.end);
    const index_zone = ausPostRates.zones.indexOf(zone.zone);
     
    const C6 = ausPostRates.fixed[index_zone];
    const D6 = ausPostRates.variable[index_zone];
    const E6 = ausPostRates.add_subsequent[index_zone]; 

    
    const average = (G4 + G5) / 2;
    const bonus = average > 42 ? E6 : 0;
    return C6 + average * D6 + bonus;
}

calculateShippingRates code

Copy the following code and paste it to a blank calculateShippingRates.js module and save it.

import { getAusPostRates } from './AusPost.js';

export async function calculateShippingRates(DATA, env) {

    const postalCode = parseInt((DATA.destination.postal_code).trim(),0);
    let rate = 0;

    DATA.items.forEach(item => {
        const itemWeight = item.grams / 1000;
        rate += getAusPostRates(postalCode,itemWeight);
    });
    
   const rates =[{
        service_name: "Australia Post",
        service_code: "EPARCEL",
        total_price: Math.round(rate * 100).toString(),
        currency: "AUD",
    }];

    return { rates };
}